﻿using Engine;
using Engine.Graphics;
using Game;
using SAGhoul.Blocks;
using System;
using System.Collections.Generic;

namespace SAGhoul.Tartareosity
{
	public class TartareosityAltarBlock : Block
	{
		readonly BlockMesh m_emptyMesh = new();
		readonly BlockMesh m_chunkedMesh = new();
		Texture2D m_texture;

		public override void Initialize()
		{
			Model model = ContentManager.Get<Model>("Models/Altar");
			m_texture = ContentManager.Get<Texture2D>("Textures/Blocks_Tartareosity");
			ModelMesh altarMesh = model.FindMesh("Altar");
			ModelMesh chunkMesh = model.FindMesh("Chunk");
			Matrix boneAbsoluteTransform1 = BlockMesh.GetBoneAbsoluteTransform(altarMesh.ParentBone);
			Matrix boneAbsoluteTransform2 = BlockMesh.GetBoneAbsoluteTransform(chunkMesh.ParentBone);
			m_emptyMesh.AppendModelMeshPart(altarMesh.MeshParts[0], Matrix.CreateTranslation(boneAbsoluteTransform1.Translation) * Matrix.CreateRotationX(-(float)Math.PI / 2f) * Matrix.CreateTranslation(0.5f, 0f, 0.5f), makeEmissive: false, flipWindingOrder: false, doubleSided: false, flipNormals: false, Color.White);
			m_chunkedMesh.AppendModelMeshPart(altarMesh.MeshParts[0], Matrix.CreateTranslation(boneAbsoluteTransform1.Translation) * Matrix.CreateRotationX(-(float)Math.PI / 2f) * Matrix.CreateTranslation(0.5f, 0f, 0.5f), makeEmissive: false, flipWindingOrder: false, doubleSided: false, flipNormals: false, Color.White);
			m_chunkedMesh.AppendModelMeshPart(chunkMesh.MeshParts[0], Matrix.CreateTranslation(boneAbsoluteTransform2.Translation) * Matrix.CreateRotationX(-(float)Math.PI / 2f) * Matrix.CreateTranslation(0.5f, 0f, 0.5f), makeEmissive: true, flipWindingOrder: false, doubleSided: false, flipNormals: false, Color.White);
			base.Initialize();
		}
		public override void GenerateTerrainVertices(BlockGeometryGenerator generator, TerrainGeometry geometry, int value, int x, int y, int z)
		{
			generator.GenerateShadedMeshVertices(this, x, y, z, IsChunked(value) ? m_chunkedMesh : m_emptyMesh, Color.White, null, null, geometry.GetGeometry(m_texture).SubsetOpaque);
		}
		public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int value, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
		{
			BlocksManager.DrawMeshBlock(primitivesRenderer, m_emptyMesh, m_texture, color, size, ref matrix, environmentData);
		}
		public override BlockDebrisParticleSystem CreateDebrisParticleSystem(SubsystemTerrain subsystemTerrain, Vector3 position, int value, float strength)
		{
			return new BlockDebrisParticleSystem(subsystemTerrain, position, strength, DestructionDebrisScale, Color.White, GetFaceTextureSlot(4, value), m_texture);
		}
		public override BoundingBox[] GetCustomCollisionBoxes(SubsystemTerrain terrain, int value)
		{
			return [new BoundingBox(Vector3.Zero, new Vector3(1f, 0.5f, 1f))];
		}
		public override int GetEmittedLightAmount(int value)
		{
			return IsChunked(value) ? 12 : 0;
		}
		public override void GetDropValues(SubsystemTerrain subsystemTerrain, int oldValue, int newValue, int toolLevel, List<BlockDropValue> dropValues, out bool showDebris)
		{
			base.GetDropValues(subsystemTerrain, oldValue, newValue, toolLevel, dropValues, out showDebris);
			if (IsChunked(oldValue))
				dropValues.Add(new BlockDropValue() { Value = typeof(BizarreMetalBlock).BlockIndex(), Count = 1 });
		}
		public static bool IsChunked(int value)
		{
			return Terrain.ExtractData(value) == 1;
		}
		public static int SetChunked(int value, bool chunked)
		{
			int data = chunked ? 1 : 0;
			return Terrain.ReplaceData(value, data);
		}
	}
}
